package com.leapp.sk.ui.activity.task

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.example.baselibrary.util.SKPermissionUtils
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.example.mvp.view.imgselector.ImageSelector
import com.example.mvp.view.video.VideoActivity
import com.example.util.BCFinalist
import com.leapp.sk.R
import com.leapp.sk.bean.CompleteTaskEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.inter.ImagePathInterface
import com.leapp.sk.ui.activity.classs.ChoseClassActivity
import com.example.util.finallist.CHOSE_CLASS_NAME
import com.example.util.finallist.CODE_205
import com.example.util.finallist.IS_TASK
import com.leapp.sk.util.getResultFile
import com.leapp.sk.util.im.listener.LKVoiceRecorderListener
import com.leapp.sk.viewdelegate.task.CompleteTaskDelegate
import java.io.File

/**
 * 完成作业
 * @author  chaohaipeng  on 2018/8/3.
 */
class CompleteTaskActivity:BaseActivityMVP<CompleteTaskDelegate>(), LKVoiceRecorderListener, ImagePathInterface {


    private val voiceData= mutableListOf<CompleteTaskEntity>()
    private val ivData= mutableListOf<CompleteTaskEntity>()
    /**
     * 是否是视频
     */
      var isVideo=false
    override fun initDate() {

    }

    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            viewDelegate.setTaskRequest()
        },R.id.iv_act_expand)
        viewDelegate.setOnClickListener(View.OnClickListener {
            viewDelegate.setVoiceVisible()

        },R.id.iv_act_chose_voice)
        viewDelegate.setOnClickListener(View.OnClickListener {
            isVideo=false
            pickImage()
        },R.id.iv_act_chose_image)

        viewDelegate.setOnClickListener(View.OnClickListener {
            val requestAudio = SKPermissionUtils.getInstance().requestAudio(this)
            val requestCamera = SKPermissionUtils.getInstance().requestCamera(this)
            if (requestAudio&&requestCamera) {
                isVideo = true
                val intent: Intent = Intent(this, VideoActivity::class.java)
                startActivityForResult(intent, BCFinalist.VIDEO_PLAY_REQUEST)
            }
        },R.id.iv_act_chose_video)

        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent=Intent(this, ChoseClassActivity::class.java)
            intent.putExtra(IS_TASK,true)
            startActivityForResult(intent, CODE_205)
        }, R.id.rl_act_chose_class)


    }

    override fun getDelegateClass(): Class<CompleteTaskDelegate> {
        return CompleteTaskDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_task_complete))
        viewDelegate?.initVoiceView(this,this)
        viewDelegate.initVoiceAdapter(voiceData)
        viewDelegate.initIVAdapter(ivData)
    }
    override fun noPermission() {
    }

    override fun amplitude(amplitude: Int) {
    }

    override fun voiceLength(voiceLength: Int) {
        viewDelegate?.setDuration(voiceLength)

    }

    override fun stopRecording(voiceLength: Int, voicePath: String?, voiceFileName: String?, voiceFile: File?) {
        ivData.clear()
        if (voicePath!=null){
            val completeTaskEntity = CompleteTaskEntity()
            completeTaskEntity.type=1
            completeTaskEntity.url=voicePath
            completeTaskEntity.duration=voiceLength
            completeTaskEntity.file=voiceFile
            voiceData.add(completeTaskEntity)
        }
        viewDelegate.notifyVoiceAdapter()

    }
    /**
     * 照片选择
     */
    private fun pickImage() {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(9)
                .setGridColumns(3)
                .setShowCamera(true)
                .startSelect(this)
    }
    override fun recordFail() {
    }
    override fun getArrayImagePath(path: List<String>) {
        voiceData.clear()
        if (isVideo){
            ivData.clear()
        }
        for (url in path) {
            val completeTaskEntity = CompleteTaskEntity()
            completeTaskEntity.type=2
            completeTaskEntity.url=url
            ivData.add(completeTaskEntity)
        }
        viewDelegate.notifyIvAdapter()
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        getResultFile(requestCode, resultCode, data, this, this)
        when(requestCode){
            BCFinalist.VIDEO_PLAY_REQUEST->{
                if (resultCode == BCFinalist.VIDEO_PLAY_REQUEST) {
                    val videoPath = data?.getStringExtra(BCFinalist.PATH_VIDEO_PATH)
                    if (videoPath!=null){
                        if (!isVideo){
                            ivData.clear()
                        }
                        val completeTaskEntity = CompleteTaskEntity()
                        completeTaskEntity.type=3
                        completeTaskEntity.url=videoPath
                        ivData.add(completeTaskEntity)
                        viewDelegate.notifyIvAdapter()
                    }
                }
            }
            CODE_205->{
                if (resultCode==CODE_205){
                    val choseEntity: String?= data?.getStringExtra(CHOSE_CLASS_NAME)
                    viewDelegate.setClass(choseEntity)
                }
            }

        }
    }
}